package com.ks.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 通过该类即可在普通工具类里获取spring管理的bean
 *
 * @author xuxiaowei
 */
public final class SpringTool implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringTool.applicationContext == null) {
            SpringTool.applicationContext = applicationContext;

            System.out.println();

            System.out.println("========" + "ApplicationContext配置成功" + "========");

            System.out.println();
        }
    }

    /**
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 获取been
     *
     * @param serviceName
     * @return
     */
    public static Object getBean(String serviceName) {
        return getApplicationContext().getBean(serviceName);
    }
}